package com.backstage.web;

import cn.hutool.json.JSONString;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.backstage.pojo.dto.Message;
import com.backstage.pojo.entity.SysPermission;
import com.backstage.pojo.entity.SysRolePermission;
import com.backstage.pojo.vo.SysRolePermissionVo;
import com.backstage.service.RolePermissionService;
import io.lettuce.core.ScriptOutputType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

@RestController
public class RolePermissionController {
    @Autowired
    RolePermissionService rolePermissionService;

    @GetMapping("rolePermission/select")
    public Message<List<SysRolePermissionVo>> select(String roleName) {
        SysRolePermissionVo sysRolePermissionVo = new SysRolePermissionVo();
        sysRolePermissionVo.setRoleName(roleName);
        sysRolePermissionVo.setPage(1);
        sysRolePermissionVo.setRows(10);
        return rolePermissionService.select(sysRolePermissionVo);
    }

    @GetMapping("rolePermission/delete")
    public Message<String> delete(SysRolePermission sysRolePermission) {
        return rolePermissionService.delete(sysRolePermission);
    }

    @GetMapping("rolePermission/add")
    public Message<String> add(SysRolePermission sysRolePermission) {
        return rolePermissionService.add(sysRolePermission);
    }

    @RequestMapping(value = "rolePermission/update", method = RequestMethod.POST)
    public Message<String> update(String sysRolePermissions) {


        List<SysRolePermission> pers = new ArrayList<>();

        Long roleid = 0L;
        String[] roleIds = sysRolePermissions.split("roleId=");
        for (int i = 0; i < roleIds.length - 1; i++) {

            if (!roleIds[i].startsWith("0.")) {
                SysRolePermission sysRolePermission = new SysRolePermission();
                int i1 = roleIds[i].indexOf("&");
                roleid = Long.valueOf(roleIds[i].substring(0, i1));
                int p = roleIds[i].indexOf("permissionId=");
                int i2 = roleIds[i].lastIndexOf("&");
                Long permissionId = Long.valueOf(roleIds[i].substring(p + 13, i2));
                sysRolePermission.setRoleId(roleid);
                sysRolePermission.setPermissionId(permissionId);
                pers.add(sysRolePermission);
            }
        }
        int i = roleIds[roleIds.length - 1].lastIndexOf("permissionId=");
        Long permission = Long.valueOf(roleIds[roleIds.length - 1].substring(i + 13));
        SysRolePermission sysRolePermission = new SysRolePermission();
        sysRolePermission.setRoleId(roleid);
        sysRolePermission.setPermissionId(permission);
        pers.add(sysRolePermission);
        rolePermissionService.update(roleid, pers);
        return Message.successMessage((long) 0, "ok");
    }

    @GetMapping("rolePermission/selectNo")
    public Message<List<SysRolePermissionVo>> selectNo(String roleName) {
        SysRolePermissionVo sysRolePermissionVo = new SysRolePermissionVo();
        sysRolePermissionVo.setPage(1);
        sysRolePermissionVo.setRows(10);
        sysRolePermissionVo.setRoleName(roleName);
        return rolePermissionService.selectNo(sysRolePermissionVo);

    }
}
